package com.sino.struts2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IAclService;

public class AclAction extends ActionSupport {
	private IAclService aclServiceImp;

	private String moduleString;// 构造的html模块字符串

	private int roleId;// 角色id

	private String[] authList = null;// 存储表单提交过来的模块列表

	/**
	 * @param aclServiceImp
	 *            the aclServiceImp to set
	 */
	public void setAclServiceImp(IAclService aclServiceImp) {
		this.aclServiceImp = aclServiceImp;
	}

	/**
	 * @return the roleId
	 */
	public int getRoleId() {
		return roleId;
	}

	/**
	 * @param roleId
	 *            the roleId to set
	 */
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	/**
	 * @return the moduleString
	 */
	public String getModuleString() {
		return moduleString;
	}

	/**
	 * @param moduleString
	 *            the moduleString to set
	 */
	public void setModuleString(String moduleString) {
		this.moduleString = moduleString;
	}

	/**
	 * @return the authList
	 */
	public String[] getAuthList() {
		return authList;
	}

	/**
	 * @param authList
	 *            the authList to set
	 */
	public void setAuthList(String[] authList) {
		this.authList = authList;
	}

	// 转向角色的权限更改页面
	public String toAuthRole() {
		authList = null;
		moduleString = this.aclServiceImp.listModuleString(roleId);
		return SUCCESS;
	}

	//调用aclServiceImp的saveOrRevokeModule函数处理角色的权限更改
	public String authRole() {
		this.aclServiceImp.saveOrRevokeModule(roleId, authList);
		//角色的权限更改完成后关闭当前页面
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("window.close();");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("角色授权异常！");
		}

		return null;
	}
}
